package com.uenpay.im.ui.old.activity.red;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.UserDetailActivity;
import com.uenpay.im.ui.old.activity.personal.MeDetailActivity;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.QyxDateUtils;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class RedDetailAdapter extends BaseAdapter {

	public interface ITanksRed {
		public abstract void onTanksRed();
	}

	private LayoutInflater inflater = null;
	private ArrayList<RedRecordEntity> entities = new ArrayList<RedRecordEntity>();
	private Context mContext;
	private SimpleDateFormat sdf = null;
	private ITanksRed tanksRed;

	public RedDetailAdapter(Context _Context,
			ArrayList<RedRecordEntity> _entities, final ITanksRed _ITanksRed) {
		entities = _entities;
		mContext = _Context;
		inflater = LayoutInflater.from(_Context);
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.getDefault());
		tanksRed = _ITanksRed;
	}

	public void setData(ArrayList<RedRecordEntity> _entities){
		entities = _entities;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return entities.size();
	}

	@Override
	public Object getItem(int arg0) {
		return entities.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		RedDetailViewHodler hodler = null;
		final RedRecordEntity redEntity = entities.get(arg0);
		if (arg1 == null) {
			hodler = new RedDetailViewHodler();
			arg1 = inflater.inflate(R.layout.item_red_record_layout, arg2,
					false);
			hodler.name = (TextView) arg1.findViewById(R.id.name_tv);
			hodler.thank_tv = (TextView) arg1.findViewById(R.id.thank_tv);
			hodler.date = (TextView) arg1.findViewById(R.id.date_tv);
			hodler.money = (TextView) arg1.findViewById(R.id.money_tv);
			hodler.imageView = (RoundedImageView) arg1
					.findViewById(R.id.avatar);
			hodler.is_bast = (LinearLayout) arg1.findViewById(R.id.is_bast);
			arg1.setTag(hodler);
		} else {
			hodler = (RedDetailViewHodler) arg1.getTag();
		}
		hodler.name.setText(redEntity.nickName);
		hodler.money.setText(MessageFormat.format(mContext.getResources()
				.getString(R.string.red_size), redEntity.recordMoney));
		hodler.imageView.SetUrl(APIConfiguration.getAvatarUrlNormal(
				redEntity.custId, PacketType.PERSONAL));
		if (redEntity.isluck == 1) {
			hodler.is_bast.setVisibility(View.VISIBLE);
		} else {
			hodler.is_bast.setVisibility(View.GONE);
		}

		if (!TextUtils.isEmpty(redEntity.thankswords)) {
			hodler.thank_tv.setText(redEntity.thankswords);
			hodler.thank_tv.setVisibility(View.VISIBLE);
			hodler.thank_tv.setTextColor(mContext.getResources().getColor(
					R.color.the_color_normal));
		} else {
			if (redEntity.custId.equals(IMApplication.getCustId())) {
				hodler.thank_tv.setVisibility(View.VISIBLE);
				hodler.thank_tv.setText(R.string.thank);
				hodler.thank_tv.setTextColor(mContext.getResources().getColor(
						R.color.the_main_color_normal));
			} else {
				hodler.thank_tv.setVisibility(View.GONE);
			}
		}

		try {
			long millionSeconds = sdf.parse(redEntity.recordTime).getTime();
			String time = QyxDateUtils.generateDateTimeString(new Date(
					millionSeconds));
			hodler.date.setText(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		hodler.thank_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO 答谢
				if (redEntity.custId.equals(IMApplication.getCustId())
						&& TextUtils.isEmpty(redEntity.thankswords))
					tanksRed.onTanksRed();
			}
		});
		hodler.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!TextUtils.isEmpty(redEntity.custId)) {

					Intent intent = new Intent();
					if (redEntity.custId.equals(IMApplication.getCustId())) {
						intent.setClass(mContext, MeDetailActivity.class);
						mContext.startActivity(intent);
					} else {
						intent.setClass(mContext, UserDetailActivity.class);
						intent.putExtra("cust_id", redEntity.custId);
						mContext.startActivity(intent);
					}

				}
			}
		});
		return arg1;
	}

	private class RedDetailViewHodler {
		private TextView name, date, money, thank_tv;
		private LinearLayout is_bast;
		private RoundedImageView imageView;
	}

}
